{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Basic Math"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## imports"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "from numpy import arange, array"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 1\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "u = array([1.0, 1, 2, 3, 5, 8])\r\n",
    "v = array([[1.0], [1], [2], [3], [5], [8]])\r\n",
    "x = array([[1.0, 0], [0, 1]])\r\n",
    "y = array([[1.0, 2], [3, 4]])\r\n",
    "z = array([[1.0, 2, 1, 2], [3, 4, 3, 4], [1, 2, 1, 3]])\r\n",
    "w = array([[1.0, 0, 1, 0], [0, 1, 0, 1], [1, 2, 1, 2], [3, 4, 3, 4]])"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"u+v.T:\")\r\n",
    "u + v.T"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"v+u:\")\r\n",
    "v + u"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"v+u.T :\")\r\n",
    "v + u.T"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "u.shape = 1, 6\n",
    "u"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"v@u:\")\n",
    "v @ u"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"u@v:\")\n",
    "u @ v"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"x@y:\")\n",
    "x @ y"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 2\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "u = array([1.0, 1, 2, 3, 5, 8])\n",
    "v = array([[1.0], [1], [2], [3], [5], [8]])\n",
    "x = array([[1.0, 0], [0, 1]])\n",
    "y = array([[1.0, 2], [3, 4]])\n",
    "z = array([[1.0, 2, 1, 2], [3, 4, 3, 4], [1, 2, 1, 3]])\n",
    "w = array([[1.0, 0, 1, 0], [0, 1, 0, 1], [1, 2, 1, 2], [3, 4, 3, 4]])\n",
    "a = array([3.0, 2])\n",
    "b = array([[3.0], [2]])\n",
    "c = array([3.0, 2, 1, 0])\n",
    "d = array([[3.0], [2], [1], [0]])\n",
    "print(\"u+b:\")\n",
    "print(u + b)\n",
    "print(\"u+d:\")\n",
    "print(u + d)\n",
    "print(\"v+a:\")\n",
    "print(v + a)\n",
    "print(\"v+c:\")\n",
    "print(v + c)\n",
    "print(\"x+a:\")\n",
    "print(x + a)\n",
    "print(\"x+b:\")\n",
    "print(x + b)\n",
    "print(\"y+a:\")\n",
    "print(y + a)\n",
    "print(\"y+b:\")\n",
    "print(y + b)\n",
    "print(\"z+c:\")\n",
    "print(z + c)\n",
    "print(\"w+c:\")\n",
    "print(w + c)\n",
    "print(\"w+d:\")\n",
    "print(w + d)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 3\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"x/1.0:\")\n",
    "print(x / 1.0)\n",
    "print(\"1.0/x:\")\n",
    "print(1.0 / x)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 4\n",
    "\n",
    "These are the same since these are jsut element-by-element.  The bottom shows\n",
    "how to do the same using linear algebra."
   ],
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "(x + y) ** 2"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x ** 2 + x * y + y * x + y ** 2"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "(x + y) @ (x + y)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x @ x + x @ y + y @ x + y @ y"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 5\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x ** 2 + 2 * x * y + y ** 2"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "x @ x + 2 * x @ y + y @ y"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 6\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "a = arange(3.0)\n",
    "b = a\n",
    "c = a"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"a*b + a*c:\")\n",
    "a * b + a * c"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"a*b + c:\")\n",
    "a * b + c"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"a*(b+c):\")\n",
    "a * (b + c)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Also works for `@`"
   ],
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"a*b + a*c:\")\n",
    "a @ b + a @ c"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"a*b + c:\")\n",
    "a @ b + c"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"a*(b+c):\")\n",
    "a @ (b + c)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 7\n",
    "\n",
    "If `x` is array, `y` must be broadcastable with `x`, \n",
    "\n",
    "`w` must be broadcastable with (`x**y`)\n",
    "\n",
    "`z` must be broadcastable with (`(x**y)*w`)\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise 10\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"-2**4:\")\n",
    "-(2 ** 4)"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"(-2)**4:\")\n",
    "(-2) ** 4"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "print(\"-2*-2*-2*-2:\")\n",
    "-2 * -2 * -2 * -2"
   ],
   "outputs": [],
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}